var city = require('../../utils/city.js');
var common = require("../../utils/common.js");
//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    city: "",
    cityList: '',
    nowCity: '',
    searchList:''
  },
  onLoad: function(options) {
    console.log(app);
    var _this = this;
    _this.setData({
      city: options.city,
      nowCity:app.globalData.city
    })
    _this.queryCityRowList();
  },
  clickLetter: function(e) {
    console.log(e.currentTarget.dataset.letter)
    var showLetter = e.currentTarget.dataset.letter;
    this.setData({
      showLetter: showLetter,
      isShowLetter: true,
      scrollTopId: showLetter,
    })
    var that = this;
    setTimeout(function() {
      that.setData({
        isShowLetter: false
      })
    }, 1000)
  },
  //选择城市
  bindCity: function(e) {
    this.setData({
      city: e.currentTarget.dataset.city
    })
    app.globalData.chooseName = e.currentTarget.dataset.city;
    wx.reLaunch({
      url: '/pages/roomDetail/roomDetail?city=' + e.currentTarget.dataset.city,
    })
  },
  //选择热门城市
  bindHotCity: function(e) {

  },
  //点击热门城市回到顶部
  hotCity: function() {
    this.setData({
      scrollTop: 0,
    })
  },

  queryCityRowList: function() {
    var _that = this;
    // var cityInfo = wx.getStorageSync('cityList');
    // if(cityInfo){
    //   console.log("缓存有")
    //   _that.setData({
    //     cityList: cityInfo,
    //     searchList: cityInfo
    //   })
    // }else{
      console.log("没有缓存")
      _that.infoGetList()
    //}
  },
  infoGetList:function(){
    var _that=this;
    // if (cityInfo && cityInfo.length > 0) {
    //   _that.setData({
    //     cityList: cityInfo,
    //     searchList: cityInfo
    //   })
    // } else {
      common.ajaxPost(
        '/portal/wlpBuilding/queryCityByXcx.action', {
          xcxId: app.globalData.queryWlpXcxByParams[0].id
        },
        function (res) {
          console.log(res);
          if (res && res.data.length > 0) {
            var cityMap = res.data;
            var citys=[];
            for (var i = 0; i < cityMap.length;i++){
              if (cityMap[i]!= "" && cityMap[i]!=null ){
                if(cityMap[i].cityName!=""){
                  citys.push(cityMap[i])
                }
              }
            }
            //缓存市区信息
            wx.setStorage({
              key: "cityList",
              data: citys
            })
            _that.setData({
              cityList: citys,
              searchList: citys
            })
          }
        })
    // } 
  },

  inputContent: function (e) {
    //input输入内容，显示取消
    var that = this;
    var indexValue = e.detail.value;
    var searchList = that.data.searchList;
    var cityList=that.data.cityList;
    var citySearchList=[];
    for (var i = 0; i < cityList.length;i++){
      if (cityList[i]!=null && cityList[i].cityName){
        if (cityList[i].cityName.indexOf(indexValue) > -1) {
          citySearchList.push(cityList[i])
        }
      }
    } 
    that.setData({
      searchList: citySearchList
    })
  },
})